// PreStiParallelPortTester.cpp : Defines the entry point for the console application.
//

#include <windows.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <winioctl.h>
#include "../PreStiPrallelDriver/GpIoctl.h"


VOID __cdecl
main(
    __in ULONG argc,
    __in_ecount(argc) PCHAR argv[]
    )
{
	HANDLE              hndFile;
	BOOL    IoctlResult;
	ULONG               ReturnedLength;
	UCHAR				buffer[2];
	LPCWSTR				LPT1 = L"LPT1:";
	LPCWSTR				LPT2 = L"LPT2:";
	LPCWSTR				LPT3 = L"LPT3:";
	LPCWSTR				LPT;
	if(argc == 3)
		buffer[0] = (char)atoi(argv[2]);
	else 
		buffer[0] = 1;
	switch(buffer[0]) {
		case 1: LPT = LPT1; break;
		case 2: LPT = LPT2; break;
		case 3: LPT = LPT3;
	}
	hndFile = CreateFile(LPT, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
	if(hndFile == INVALID_HANDLE_VALUE) {
		printf("Port LPT%ld is not present\n", buffer[0]);
		return;
	}
	CloseHandle(hndFile);

	hndFile = CreateFile(L"\\\\.\\GpdDev", // device name
		GENERIC_READ|GENERIC_WRITE,
		0,
		NULL,
		OPEN_EXISTING,
		0,
		NULL);
	if(hndFile == INVALID_HANDLE_VALUE) {
		printf("PreSti Parallel Port Driver is not insalled\n", buffer[0]);
		return;
	}
	buffer[1] = 0;
	if(argc >= 2)
		buffer[1] = (char)atoi(argv[1]);
	IoctlResult = DeviceIoControl(
                        hndFile,                // Handle to device
                        IOCTL_PRESTI_SEND_BYTE,              // IO Control code for Write
                        buffer,           // Buffer to driver.  Holds port & data.
                        2,             // Length of buffer in bytes.
                        NULL,                   // Buffer from driver.   Not used.
                        0,                      // Length of buffer in bytes.
                        &ReturnedLength,        // Bytes placed in outbuf.  Should be 0.
                        NULL                    // NULL means wait till I/O completes.
                        );
	if (!IoctlResult)                            // Did the IOCTL succeed?
	{
		printf("Cannot write to parallel port (%d)", GetLastError());
	}

	CloseHandle(hndFile);
}

